package com.troyhigh.njrotc.admintrackerold;

import java.util.ArrayList;
import java.sql.Date;
import java.util.Collections;

public class SQLDatabaseAL extends SQLDatabase implements Database
{
    private static ArrayList<Student> students;
    private static ArrayList<Event> events;
    
    @Override
    public void connect()
    {
        super.connect();
        events = super.getEvents();
        Collections.sort(events);
        students = super.getStudents();
        Collections.sort(students);
    }
    
    @Override
    public void disconnect()
    {
        System.out.println("Applying changes...");
        setStudents(students);
        setEvents(events);
        super.disconnect();
        System.out.println("Changes successfully made.");
    }
    
    public void committ()
    {
        setStudents(students);
        setEvents(events);
    }
    
    @Override
    public ArrayList<Student> getStudents()
    {
        return students;
    }
    
    @Override
    public ArrayList<Event> getEvents()
    {
        return events;
    }
    
    @Override
    public ArrayList<Student> getStudents(String l, String f, int id)
    {
        ArrayList<Student> result = new ArrayList<Student>();
        if(id <= 0 && (f == null && f.length()==0) && (l == null || l.length() == 0)) return null;
        for(Student s : students)
        {
            if((id > 0 ? id == s.getID() : true) &&
               (f.length() > 0 ? f.equals(s.getFirstName()) : true) &&
               (l.length() > 0 ? l.equals(s.getLastName()) : true))
            result.add(s);
        }
        return result;
    }
    
    @Override
    public ArrayList<Event> getEvents(String n, Date d)
    {
        ArrayList<Event> result = new ArrayList<Event>();
        if(n == null && n.length() < 1 && d == null) return null;
        for(Event e : events)
        {
            if(n.length() >= 1 ? n.equals(e.getName()) : true &&
               d != null ? d.equals(e.getDate()) : true)
            result.add(e);
        }
        return result;
    }
    
    @Override
    public Student getStudent(String l, String f, int id)
    {
        ArrayList<Student> s = getStudents(f, l, id);
        if(s != null && s.size() > 0)
            return s.get(0);
        return null;
    }
    
    @Override
    public Event getEvent(String n, Date d)
    {
        ArrayList<Event> e = getEvents(n, d);
        if(e != null && e.size() > 0)
            return e.get(0);
        return null;
    }
    
    @Override
    public boolean studentExists(Student s)
    {
        return students.contains(s);
    }
    
    @Override
    public boolean eventExists(Event e)
    {
        return events.contains(e);
    }
    
    @Override
    public void setStudents(ArrayList<Student> ss)
    {
        for(Student s : ss)
        {
            super.setStudent(s);
        }
    }
    
    @Override
    public void setEvents(ArrayList<Event> es)
    {
        for(Event e : es)
            super.setEvent(e);
    }
    
    @Override
    public void setStudent(Student s)
    {
        if(s == null) return;
        Student gs = getStudent("", "", s.getID());
        if(gs != null)
            students.set(students.indexOf(gs), s);
        else
            students.add(s);
        Collections.sort(students);
    }
    
    @Override
    public void setEvent(Event e)
    {
        if(e == null) return;
        Event ge = getEvent(e.getName(), e.getDate());
        if(ge != null)
            events.set(events.indexOf(ge), e);
        else
            events.add(e);
        super.setRoster(e);
        Collections.sort(events);
    }
    
    //public void setRoster(Event e)
    {
        //because the events are modified at the end, there is no need to set
        //the roster... ever.
    }
    
    @Override
    public void removeStudent(String l, String f, int id)
    {
        Student s = this.getStudent(f, l, id);
        if(s == null) return;
        students.remove(students.indexOf(s));
    }
    
    @Override
    public void removeEvent(String n, Date d)
    {
        Event e = this.getEvent(n, d);
        if(e == null) return;
        events.remove(events.indexOf(e));
    }
    
    
}